﻿using System;
using System.Drawing;
using System.IO;
using System.Windows.Forms;

namespace KXmlUIControl
{
    /// <summary>
    /// XML控件对象基类
    /// </summary>
    public abstract class XmlControlBase
    {
        public string Name {get; set;}
        public string ForeColor { get; set; }
        public string BackColor { get; set; }
        public string Font { get; set; }
        public string Size { get; set; }
        public string Location { get; set; }
        public bool Enabled { get; set; }
        public bool Visible { get; set; }
        public string Text { get; set; }
        public string Tag { get; set; }
        public string BackgroundImage { get; set; }
        public string TabIndex { get; set; }
        public abstract string ToXMLString();
        public abstract Control ToControl(object parent);

        public XmlControlBase()
        {
            Enabled = true;
            Visible = true;
        }

        public void SetName(Control ctl)
        {
            if (string.IsNullOrEmpty(Name))
            {
                return;
            }
            ctl.Name = Name;
        }

        public void SetTabIndex(Control ctl)
        {
            if (string.IsNullOrEmpty(TabIndex))
            {
                return;
            }
            int value = 0;
            if (int.TryParse(TabIndex, out value))
            {
                ctl.TabIndex = value;
            }
        }

        public void SetForeColor(Control ctl)
        {
            if (string.IsNullOrEmpty(ForeColor))
            {
                return;
            }
            Color value = StaticTypes.GetColor(ForeColor);
            if (value != Color.Empty)
            {
                ctl.ForeColor = value;
            }
        }

        public void SetBackColor(Control ctl)
        {
            if (string.IsNullOrEmpty(BackColor))
            {
                return;
            }
            Color value = StaticTypes.GetColor(BackColor);
            if (value != Color.Empty)
            {
                ctl.BackColor = value;
            }
        }

        public void SetSize(Control ctl)
        {
            if (string.IsNullOrEmpty(Size))
            {
                return;
            }
            Size value = StaticTypes.GetSize(Size);
            if (value != System.Drawing.Size.Empty)
            {
                ctl.Size = value;
            }
        }

        public void SetFont(Control ctl)
        {
            if (string.IsNullOrEmpty(Font))
            {
                return;
            }
            Font value = StaticTypes.GetFont(Font);
            if (value != null)
            {
                ctl.Font = value;
            }
        }

        public void SetLocation(Control ctl)
        {
            if (string.IsNullOrEmpty(Location))
            {
                return;
            }
            Point value = StaticTypes.GetLocation(Location);
            if (value != Point.Empty)
            {
                ctl.Location = value;
            }
        }

        public void SetBackgroundImage(Control ctl)
        {
            if (string.IsNullOrEmpty(BackgroundImage))
            {
                return;
            }
            Image image = null;
            if (ctl is Button)
            {
                image = StaticTypes.GetButtonImage(BackgroundImage);
                if (image == null)
                {
                    image = StaticTypes.GetIconImage(BackgroundImage);
                }
            }
            else if (ctl is PictureBox)
            {
                image = StaticTypes.GetPageGifImage(BackgroundImage);
                if (image == null)
                {
                    image = StaticTypes.GetIconImage(BackgroundImage);
                }
            }
            else
            {
                Type baseType = ctl.GetType().BaseType;
                if (baseType == null)
                {
                    return;
                }
                if (baseType == typeof(Form))
                {
                    image = StaticTypes.GetWindowImage(BackgroundImage);
                }
                else
                {
                    baseType = baseType.BaseType;
                    if (baseType != null && (baseType == typeof(UserControl) || baseType == typeof(Form)))
                    {
                        image = StaticTypes.GetWindowImage(BackgroundImage);
                    }
                }
            }                     
            if (image != null)
            {
                ctl.BackgroundImage = image;
                ctl.BackgroundImageLayout = ImageLayout.Stretch;
            }
        }

        public void SetEnabled(Control ctl)
        {
            ctl.Enabled = Enabled;          
        }

        public void SetVisible(Control ctl)
        {
            ctl.Visible = Visible;           
        }

        public void SetText(Control ctl)
        {
            if (string.IsNullOrEmpty(Text))
            {
                return;
            }
            if (Text.Contains("\\n"))
            {
                Text = Text.Replace("\\n", "\n");
            }
            ctl.Text = Text;
        }

        public void SetTag(Control ctl)
        {
            if (string.IsNullOrEmpty(Tag))
            {
                return;
            }
            ctl.Tag = Tag;
        }

        public virtual void SetValues(Control ctl)
        {
            SetTabIndex(ctl);
            SetBackColor(ctl);
            SetBackgroundImage(ctl);
            SetEnabled(ctl);
            SetFont(ctl);
            SetForeColor(ctl);
            SetLocation(ctl);
            SetName(ctl);
            SetSize(ctl);
            //if (!(ctl is TextBox))
            //{
            //    SetSize(ctl);
            //}           
            SetTag(ctl);
            SetText(ctl);
            SetVisible(ctl);
        }

        public static string GetVocieFullPath(string voice, string languageCode = "zh-CN")
        {
            if (string.IsNullOrEmpty(voice))
            {
                return "";
            }         
            if (voice.EndsWith(".mp3") || voice.EndsWith(".wav"))
            {
                return Path.Combine(StaticTypes.VoiceFolder, languageCode, voice);
            }
            else
            {
                return string.Format("{0}\\{1}\\{2}.mp3", StaticTypes.VoiceFolder, languageCode, voice);
            }
        }
    }
}
